﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Xml;

namespace DotNet.Common
{
   public class LogHelper
    {
       
       /// <summary>
       /// 按字符生成日志 Web使用
       /// </summary>
       /// <param name="page">this</param>
       /// <param name="err">string</param>
       public static void WriteWebLogInfo(System.Web.UI.Page page, string err)
       {
           //应用程序出错日志 按天生成         
           string strErr = "<item>\r\n<user_ip>" + page.Request.UserHostAddress + "</user_ip>\r\n<err_url>" 
                           + page.Request.Url.ToString() +"</err_url>\r\n<err_message>" 
                           + err + "</err_message>\r\n" 
                           + "<create_time>" 
                           + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 
                           + "</create_time>\r\n</item>\r\n";
           //+"</br>\n Stack Trace:" + objErr.StackTrace.ToString();
           string fileName = "WebLog_" + DateTime.Now.ToString("yyyyMMdd") + ".config";
           string errPah = FileHelper.GetMapPath("~/Log/" + fileName);
           FileHelper.FileAdd(errPah, strErr);
       }

       /// <summary>
       /// 按异常生成日志 Web使用
       /// </summary>
       /// <param name="page">this</param>
       /// <param name="ex">Ex</param>
       public static void WriteWebExLogInfo(System.Web.UI.Page page, Exception ex)
       {
           //应用程序出错日志 按天生成           
           Exception objErr = ex;
           string err = "<item>\r\n<user_ip>" + page.Request.UserHostAddress 
               + "</user_ip>\r\n<err_url>" + page.Request.Url.ToString() 
               + "</err_url>\r\n<err_message>" + objErr.Message.ToString() 
               + "</err_message>\r\n" 
               + "<Source>" + objErr.Source + "</Source>\r\n"
               + "<StackTrace>"+ex.StackTrace.ToString()+"</StackTrace>\r\n"
               + "<TargetSite>"+ex.TargetSite.ToString()+"</TargetSite>\r\n"
               + "<create_time>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</create_time>\r\n</item>\r\n";
           //+"</br>\n Stack Trace:" + objErr.StackTrace.ToString();
           string fileName = "WebExLog_" + DateTime.Now.ToString("yyyyMMdd") + ".config";
           string errPah = FileHelper.GetMapPath("~/Log/" + fileName);
           FileHelper.FileAdd(errPah, err);
       }

       /// <summary>
       /// 按字符生成日志 
       /// </summary>      
       /// <param name="err">string</param>
       public static void WriteLogInfo(string err)
       {
           //应用程序出错日志 按天生成         
           string strErr = "<item>\r\n<err_message>" + err + "</err_message>\r\n"               
               + "<create_time>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</create_time>\r\n</item>\r\n";
           string fileName = "Log_" + DateTime.Now.ToString("yyyyMMdd") + ".config";
           string errPah = FileHelper.GetMapPath("~/Log/" + fileName);
           FileHelper.FileAdd(errPah, strErr);
       }

       /// <summary>
       /// 按异常生成日志 
       /// </summary>  
       /// <param name="ex">Ex</param>
       public static void WriteExLogInfo(Exception ex)
       {
           //应用程序出错日志 按天生成           
           Exception objErr = ex;
           string err = "<item>\r\n<err_message>" + objErr.Message.ToString() + "</err_message>\r\n" 
               +"<Source>" + objErr.Source + "</Source>\r\n"
               + "<StackTrace>" + ex.StackTrace.ToString() + "</StackTrace>\r\n"
               + "<TargetSite>" + ex.TargetSite.ToString() + "</TargetSite>\r\n"
               + "<create_time>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 
               + "</create_time>\r\n</item>\r\n";
           string fileName = "ExLog_" + DateTime.Now.ToString("yyyyMMdd") + ".config";
           string errPah = FileHelper.GetMapPath("~/Log/" + fileName);
           FileHelper.FileAdd(errPah, err);
       }
    }
}
